const app = getApp()
import {
  getOrderList,
  refundRequest,
  retryPay,
  cancelPay,
  queryPayResult
} from "../../common/api";

Page({
  data: {
    hideSetting: true,
    isRefresh: false,
    currentTab: 0,
    taskList: [{
      name: '打印订单'
    }],
    orderList: [],
    d_page: 1,
    z_page: 0,
    wudingdao: true
  },
  onLoad() {
    this.get_or_list();
  },
  onRefresh: function () {
    //导航条加载动画
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000);
  },
  handleClick(e) {
    let currentTab = e.currentTarget.dataset.index
    this.setData({
      currentTab
    })
  },
  handleSwiper(e) {
    let {
      current,
      source
    } = e.detail
    if (source === 'autoplay' || source === 'touch') {
      const currentTab = current
      this.setData({
        currentTab
      })
    }
  },
  handleTolower(e) {
    if (this.data.d_page >= this.data.z_page) {
      wx.showToast({
        title: '到底啦'
      })
    } else {
      this.setData({
        d_page: this.data.d_page + 1
      });
      this.get_or_list();
    }
  },
  refresherpulling() {
    wx.showLoading({
      title: '刷新中'
    })
    this.setData({
      or_list: [],
      d_page: 1,
      z_page: 0
    });
    this.get_or_list(1);
  },
  get_or_list(type = 0) {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    var _this = this;
    if (type == 1) {
      _this.setData({
        orderList: [],
        z_page: 1
      });
    }
    let data = {
      pageNum: _this.data.d_page,
      pageSize: 10,
      orderByColumn: "createTime",
      isAsc: "desc"
    }
    getOrderList(data).then(res => {
      wx.hideLoading();
      if (res.rows.length == 0) {
        _this.setData({
          isRefresh: false
        })
        wx.showToast({
          title: '加载完成'
        })
      } else {
        if (res.total > 0) {
          _this.setData({
            orderList: _this.data.orderList.concat(res.rows),
            z_page: parseInt((res.total + 10 - 1) / 10)
          });
        } else {
          _this.setData({
            wudingdao: false
          })
        }
      }
    }).catch(err => {
      console.log(err)
      wx.hideLoading();
    })
  },
  onContact: function (t) {
    var e = t.currentTarget.dataset.phone;
    console.log(t);
    wx.makePhoneCall({
      phoneNumber: e
    });
  },
  goPrint: function (t) {
    let orderId = t.currentTarget.dataset.jobId;
    let printerId = t.currentTarget.dataset.printerId;
    console.log("打印机ID", printerId)
    console.log("订单Id", orderId)
    let queryPay = {
      "orderId": orderId
    }
    wx.showLoading({
      title: '申请打印成功中...',
      mask: true,
    })
    queryPayResult(queryPay).then(res => {
      wx.hideLoading();
      console.log("支付状态", res);
      if (res.code == 200) {
        setTimeout(_ => {
          wx.redirectTo({
            url: '/pages/printing/index?orderId=' + orderId + "&printerId=" + printerId,
          })
        }, 500)
      }
    })
  },
  cancelOrder: function (t) {
    let orderId = t.currentTarget.dataset.jobId;
    let cancelData = {
      "orderId": orderId
    }
    wx.showLoading({
      title: '关闭订单中...',
      mask: true,
    })
    cancelPay(cancelData).then(res => {
      setTimeout(() => {
        wx.hideLoading();
        this.get_or_list(1);
        wx.showToast({
          title: '关闭成功',
          icon: "none",
          mask: true,
        })
      }, 1000)
    })
  },
  viewCopyTextClick: function (t) {
    let orderId = t.currentTarget.dataset.jobId;
    console.log(orderId)
    wx.setClipboardData({
      data: orderId,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '订单号复制成功'
            })
          }
        })
      }
    })
  },
  goPay: function (t) {
    let orderId = t.currentTarget.dataset.jobId;
    let data = {
      "orderId": orderId
    }
    console.log("重新调起支付", data);
    wx.showLoading({
      title: '调起支付中...',
      mask: true,
    }),
      retryPay(data).then(res => {
        wx.hideLoading();
        res.data.orderV3.package = res.data.orderV3.packageValue
        if (res.code == 200) {
          //调起支付
          wx.requestPayment(Object.assign(res.data.orderV3, {
            success: res => {
              let queryPay = {
                "orderId": orderId
              }
              queryPayResult(queryPay).then(res => {
                console.log("支付状态", res);
                if (res.code == 200) {
                  this.setData({
                    payment_success: true,
                  })
                  wx.showToast({
                    title: '支付成功',
                    mask: true,
                  })
                  setTimeout(_ => {
                    wx.redirectTo({
                      url: '/pages/printing/index?orderId=' + orderId,
                    })
                  }, 500)
                }
              })
            },
            fail: err => {
              console.log("ERR", err)
              wx.showToast({
                title: '支付失败',
                icon: 'error',
                mask: true,
              })
            }
          }))
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            mask: true,
          })
        }
      })
  },
  onRefund: function (t) {

    let e = t.currentTarget.dataset.jobId;
    console.log(e);
    this.setData({
      jobId: e,
      refundDesc: "",
      hideSetting: false
    });
  },
  close: function () {
    this.setData({
      hideSetting: true
    });
  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  navigatorToPickUp(e) {
    let index = e.currentTarget.dataset.index;
    let order = this.data.orderList[index];
    console.log(order);
    let lat = parseFloat(order.pickUpLat);
    let lng = parseFloat(order.pickUpLng);
    wx.openLocation({
      latitude: lat,
      longitude: lng,
    })
  },
  btnHover: function () {
    let data = {
      orderId: this.data.jobId,
      refundDesc: this.data.inputValue
    }
    wx.showLoading({
      title: '申请退款中...',
      mask: true,
    })
    console.log("退款信息", data);
    refundRequest(data).then(res => {
      wx.hideLoading();
      console.log(res);
      wx.showToast({
        title: res.msg,
        mask: true,
        duration: 2000,
      })
      if (res.code == 200) {
        this.setData({
          or_list: [],
          d_page: 1,
          z_page: 0
        });
        this.get_or_list(1);
      }
    })
    this.setData({
      hideSetting: true
    });
    console.log("this", this);
  },
})